package com.arkopp.partyscanner;

import java.util.ArrayList;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.util.Log;


public class EventXMLHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	private boolean inResponse = false;
	private boolean inEvent = false;
	private boolean inID = false;
	private boolean inName = false;
	
	
	private String tmpID;
	private String tmpName;
	private ArrayList<Event> events;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ArrayList<Event> getParsedData() {
		return events;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		// Nothing to do
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("response")) {
			inResponse = true;
		} else if (localName.equals("event")) {
			inEvent = true;
		} else if (localName.equals("id")) {
			inID = true;
		}else if (localName.equals("name")) {
			inName = true;
		}
	}

	/** Gets be called on closing tags like:
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("response")) {
			inResponse = false;
		}else if (localName.equals("event")) {
			inEvent = false;
			Event e = new Event(tmpID, tmpName);
			events.add(e);
		}else if (localName.equals("id")) {
			inID = false;
		}else if (localName.equals("name")) {
			inName = false;
		}
	}

	/** Gets be called on the following structure:
	 * <tag>characters</tag> */
	@Override
	public void characters(char ch[], int start, int length) {
		if (inName) {
			tmpName = new String(ch, start, length);
		} else if (inID) {
			tmpID = new String(ch, start, length);
		}
	}
}